package com.sitech.openapi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import com.sitech.openapi.domain.AsyncFeedBack;
import com.sitech.openapi.domain.Test;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LiJie
 * @Description
 * @date 2018/11/29
 */
@RestController
@Api(tags = "异步反馈",description = "异步反馈接口控制层")
@Slf4j
public class AsyncFeedBackController {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private SetHttpHeaderCmpt setHttpHeaderCmpt;
    @PostMapping("/asyncFeedBack")
    @ApiOperation(value = "异步反馈",notes = "异步反馈")
    public ResponseEntity<Test> asyncFeedBack(@RequestBody AsyncFeedBack asyncFeedBack,
                                              @RequestHeader HttpHeaders headers){
        try {
            log.info("入参<><><>><>{}",objectMapper.writeValueAsString(asyncFeedBack));
        } catch (JsonProcessingException e) {

        }
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<Test> res = new ResponseEntity<Test>(null,httpHeaders, HttpStatus.OK);
        return  res;
    }
    @ExceptionHandler
    public ResponseEntity<Map<String, String>> asyncFeedBackError(Exception e, NativeWebRequest request) {
        Map<String, String> errors = new HashMap<>();
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message",e.toString());
        errors.put("referenceError","");

        HttpHeaders httpHeaders = setHttpHeaderCmpt.setErrorHttpHeaders(request);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, httpHeaders, HttpStatus.BAD_REQUEST);
        return re;
    }
}
